package com.qixia.sportsupload.util;

import com.alibaba.fastjson.JSON;
import com.creek.data.CameCaseUtil;
import com.creek.data.DataMap;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Administrator
 * @Date 2023/12/12
 * @Description
 */
public class PageUtil {

    private static final String ID_SPLIT_CHAR = ",";

    private PageUtil() {
        throw new IllegalStateException("Utility class");
    }

    public static void setPageInfo(Integer page, Integer pageSize) {
        //分页查询
        if (page == null || page <= 0) {
            page = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 20;
        }
        PageHelper.startPage(page, pageSize);
    }

    public static DataMap pageFormat(Integer page, Integer pageSize, List<?> dataList) {
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = 20;
        }
        long total = new PageInfo<>(dataList).getTotal();
        return setUpMap(dataList, page, pageSize, total);
    }

    public static DataMap pageFormat(Integer page, Integer pageSize, List<?> newList, List<?> oldList) {
        if (page == null || page <= 0) {
            page = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 20;
        }
        long total = new PageInfo<>(oldList).getTotal();
        return setUpMap(newList, page, pageSize, total);
    }

    private static DataMap setUpMap(List<?> dataList, int page, int pageSize, long total) {
        DataMap dataMap = new DataMap();
        if (dataList != null) {
            dataMap.put("rows", dataList);
        } else {
            dataMap.put("rows", new ArrayList<>());
        }
        boolean hasNext = total - (long) page * pageSize > 0;
        dataMap.put("total", total);
        dataMap.put("hasNext", hasNext);
        dataMap.put("page", page);
        return dataMap;
    }

    public static Integer getPage(Map<String, Object> queryData) {
        Integer pageNumber = (Integer) queryData.get("pageNumber");
        if (pageNumber == null) {
            pageNumber = 1;
        }
        return pageNumber;
    }

    public static Integer getPageSize(Map<String, Object> queryData) {
        Integer pageSize = (Integer) queryData.get("pageSize");
        if (pageSize == null) {
            pageSize = 20;
        }
        return pageSize;
    }

    public static String getParams(Map<String, Object> queryData, String p) {
        String temp = (String) queryData.get(p);
        if (temp == null) {
            temp = "";
        }
        return temp;
    }

    public static DataMap pageFormat(List<?> dataList) {
        DataMap dataMap = new DataMap();
        if (dataList == null) {
            dataList = new ArrayList<>();
        }
        dataMap.put("rows", dataList);
        dataMap.put("total", dataList.size());
        return dataMap;
    }

    public static String packOrder(String orderStr) {
        String order = "";
        if (!StringUtils.isEmpty(orderStr)) {
            String[] orderArray = orderStr.split(ID_SPLIT_CHAR);
            StringBuilder sb = new StringBuilder();
            for (String s : orderArray) {
                String castStr = CameCaseUtil.toUnderScoreCase(s);
                if (sqlInjectValid(castStr)) {
                    if (s.endsWith("+")) {
                        castStr = castStr.replace("+", "");
                        sb.append(castStr).append(ID_SPLIT_CHAR);
                    } else if (s.endsWith("-")) {
                        castStr = castStr.replace("-", " desc");
                        sb.append(castStr).append(ID_SPLIT_CHAR);
                    }
                }
            }
            if (sb.length() > 0) {
                order = sb.deleteCharAt(sb.length() - 1).toString();
            }
        }
        return order;
    }

    public static Map<String, Object> packSearch(DataMap search) {
        Map<String, Object> searchMap = new HashMap<>(2);
        if (!CollectionUtils.isEmpty(search)) {
            for (Map.Entry<String, Object> entry : search.entrySet()) {
                String key = entry.getKey().toLowerCase();
                if (sqlInjectValid(key) && !StringUtils.isEmpty(entry.getValue())) {
                    searchMap.put(CameCaseUtil.toUnderScoreCase(entry.getKey()), entry.getValue());
                }
            }
        }
        return searchMap;
    }

    public static boolean sqlInjectValid(String key) {
        String[] regArray = SQL_REX.split("\\|");
        boolean flag = false;
        for (String s : regArray) {
            if (key.contains(s)) {
                flag = true;
                break;
            }
        }
        return !flag;
    }

    public static Map<String, Object> packSearch(String searchStr) {
        DataMap dataMap = JSON.parseObject(searchStr, DataMap.class);
        return packSearch(dataMap);
    }

    public final static String SQL_REX = "'| and |exec |execute |insert |select |delete |update | count|drop |\\*|%|chr |mid |master |truncate |" +
            "char |declare |sitename|net user|xp_cmdshell|;| or |,|like'|create |" +
            "table | from |grant |use |group_concat|column_name|" +
            "information_schema.columns|table_schema|union |where | order | by |--| like |//|/|#";

    public static DataMap getPageInfo(int page, int pageSize, List<Object> list) {
        if (page <= 0) {
            page = 0;
        } else {
            page = page - 1;
        }
        DataMap result = new DataMap();
        if (!CollectionUtils.isEmpty(list)) {
            Object o = list.get(0);
            List<Object> dataList = JSON.parseArray(JSON.toJSONString(o), Object.class);
            int count = dataList.size();
            int fromIndex = page * pageSize;
            int toIndex = (page + 1) * pageSize;
            int maxPage = dataList.size() / pageSize;
            boolean hasNext = false;
            if (dataList.size() % pageSize != 0) {
                maxPage = maxPage + 1;
            }
            if (page > maxPage) {
                page = maxPage;
            } else {
                hasNext = true;
            }
            if (toIndex > count) {
                toIndex = count;
            }
            if (fromIndex > count) {
                fromIndex = count;
            }
            result.put("total", count);
            result.put("hasNext", hasNext);
            result.put("page", page);
            dataList = dataList.subList(fromIndex, toIndex);
            result.put("rows", dataList);
        } else {
            result.put("rows", new ArrayList<>());
            result.put("total", 0);
            result.put("hasNext", false);
            result.put("page", page);
        }
        return result;
    }
}
